﻿<?php                
//$GLOBALS['THRIFT_ROOT'] = '/var/www/domains/Framework/thrift';

require_once 'thrift/Thrift.php';
require_once 'thrift/protocol/TBinaryProtocol.php';
require_once 'thrift/transport/THttpClient.php';
require_once 'thrift/transport/TSocket.php';
require_once 'thrift/transport/TBufferedTransport.php';
require_once 'thrift/packages/scrible/TTLog.php';

class ThriftLogger
{
	private static $_thrift_config=null;
	
	public static function set_thrift_config($cfg)
	{
        
		self::$_thrift_config=array();
                
		foreach ($cfg as $c)
		{
			$t=array("server" => $c->server, "port" => $c->port, "uri" => $c->uri, "transport" => $c->transport, "enabled" => $c->enabled);
			self::$_thrift_config[]=$t;
		}
        
	}
	
	public static function thrift_send_log($module, $action, $uid, $remote_host, $data, $index=0)
	{
        $thrift = Zend_Registry::get("thrift");                
     
        if (self::$_thrift_config==null)
		{
            self::set_thrift_config(Zend_Registry::get("thrift"));			
            
		}
		
		$thrift_server=self::$_thrift_config[$index]["server"];
		$thrift_port=self::$_thrift_config[$index]["port"];
		$thrift_uri=self::$_thrift_config[$index]["uri"];
		$thrift_transport=self::$_thrift_config[$index]["transport"];
		$thrift_enabled=self::$_thrift_config[$index]["enabled"];
		
		if (!$thrift_enabled)
		{
			return 0;
		}
		
		if ($thrift_transport=="http")
		{
			$socket = new THttpClient($thrift_server, $thrift_port, $thrift_uri);
		}
		else
		{
			$socket = new TSocket($thrift_server, $thrift_port);
		}
		
		$transport = new TBufferedTransport($socket, 1024, 1024);
		$protocol = new TBinaryProtocol($transport);
		$client = new TTLogClient($protocol);

		try
		{
			$transport->open();
			$client->async_send($module, $action, $uid, $remote_host, $data);
			$transport->close();
		}
		catch(exception $e)
		{
	        return -1;
		}
		return 1;
        
	}
	
	public static function thrift_log($action, $data, $module = 'mytt', $index=0)
	{

		$u=Zend_Registry::get("user");
		
		if (isset($_SERVER["HTTP_X_FORWARDED_FOR"])) 
			$ip = $_SERVER["HTTP_X_FORWARDED_FOR"];
		else 
			$ip = $_SERVER["REMOTE_ADDR"];
		self::thrift_send_log($module, $action, $u->uid, $ip, serialize($data), $index);
                
	}
	
	public static function thrift_log_get_content($array_content) {
        return "";
		$action = array_shift($array_content);
		
		$temp_arr = explode("/", $action);
		
		if (($temp_arr[0] == 'network') || ($temp_arr[0] == 'group') || ($temp_arr[0] == 'event') || ($temp_arr[0] == 'bookmark') || ($temp_arr[0] == 'forum'))
			$module = $temp_arr[0];
		else
			$module = 'mytt';
		
		self::thrift_log($action, $array_content, $module);
	}
}


